package com.yowasa.xiuxian.service.helper;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.po.*;
import com.yowasa.xiuxian.mapper.*;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskHelper {

    @Autowired
    private XiuxianTaskInfoMapper xiuxianTaskInfoMapper;

    @Autowired
    private XiuxianNpcInfoMapper xiuxianNpcInfoMapper;

    @Autowired
    private XiuxianUserTaskMapper xiuxianUserTaskMapper;

    public List<XiuxianNpcTab> getAllNpc(Integer local) {
        return xiuxianNpcInfoMapper.selectList(new LambdaQueryWrapper<XiuxianNpcTab>().eq(XiuxianNpcTab::getMapId,local));
    }

    public XiuxianUserTask getNowTakByUserId(Long userId) {
        List<XiuxianUserTask> xiuxianUserTasks = xiuxianUserTaskMapper.selectList(new LambdaQueryWrapper<XiuxianUserTask>().eq(XiuxianUserTask::getAccountId, userId).eq(XiuxianUserTask::getStatus, 0).last("limit 1"));
        if (xiuxianUserTasks.size() == 0){
            return null;
        }else {
            XiuxianUserTask xiuxianUserTask = xiuxianUserTasks.get(0);
            XiuxianTaskTab xiuxianTaskTab = xiuxianTaskInfoMapper.selectById(xiuxianUserTask.getTaskId());
            xiuxianUserTask.setXiuxianTaskTab(xiuxianTaskTab);
            return xiuxianUserTask;
        }
    }

    public void updateTaskStage(XiuxianTaskTab task, UserBO userBO) {
        xiuxianUserTaskMapper.updateFinishByUserTask(task.getId(),userBO.getId());
        XiuxianUserTask xiuxianUserTask = new XiuxianUserTask();
        xiuxianUserTask.setAccountId(userBO.getId());
        xiuxianUserTask.setStatus(0);
        xiuxianUserTask.setCreate_time(new Date());
        xiuxianUserTask.setTaskId(task.getNextId());
        xiuxianUserTaskMapper.insert(xiuxianUserTask);
    }

    public XiuxianTaskTab getRandomTaskByUser(UserBO userBO) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        Date time = calendar.getTime();
        List<XiuxianUserTask> xiuxianUserTasks = xiuxianUserTaskMapper.selectList(new LambdaQueryWrapper<XiuxianUserTask>().eq(XiuxianUserTask::getAccountId, userBO.getId()).gt(XiuxianUserTask::getCreate_time, time));
        if (xiuxianUserTasks.size() != 0){
            return null;
        }
        XiuxianTaskTab xiuxianTaskTab = xiuxianTaskInfoMapper.selectRandomByLevel(userBO.getStage());
        XiuxianUserTask xiuxianUserTask = new XiuxianUserTask();
        xiuxianUserTask.setStatus(0);
        xiuxianUserTask.setTaskId(xiuxianTaskTab.getId());
        xiuxianUserTask.setCreate_time(new Date());
        xiuxianUserTask.setAccountId(userBO.getId());
        xiuxianUserTaskMapper.insert(xiuxianUserTask);
        return xiuxianTaskTab;
    }


    public XiuxianNpcTab getNpcById(Integer npcId) {

        return xiuxianNpcInfoMapper.selectById(npcId);
    }


    public void completeTask(XiuxianUserTask nowTakByUserId) {
        nowTakByUserId.setStatus(1);
        xiuxianUserTaskMapper.updateById(nowTakByUserId);
    }
}
